//
// Created on 2025/3/11.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include <EGL/egl.h>
#include <GLES3/gl3.h>
#include <iostream>
// 在文件头部添加鸿蒙NDK头文件
#include <hilog/log.h>
#include <native_buffer/native_buffer.h>
#include <surface/surface.h>
#include <ui/rs_surface_node.h>

// 定义纹理结构体
struct TextureWrapper {
    GLuint textureID;
    int width;
    int height;
};

// 创建摄像头纹理（需要鸿蒙相机权限）
TextureWrapper createCameraTexture() {
    TextureWrapper tex;
    glGenTextures(1, &tex.textureID);
    
    // 获取鸿蒙相机数据（示例伪代码）
    OH_Camera* camera = OH_Camera_Create();
    OH_Camera_Start(camera);
    
    // 创建Surface绑定到纹理
    OHNativeWindow* window = OH_Camera_GetSurface(camera);
    EGLSurface surface = eglCreateWindowSurface(display, config, window, NULL);
    
    // 配置纹理参数
    glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex.textureID);
    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    return tex;
}

// 创建ArkUI组件纹理
TextureWrapper createArkUITexture() {
    TextureWrapper tex;
    glGenTextures(1, &tex.textureID);
    
    // 创建离屏Surface
    OHNativeWindow* window = OH_NativeWindow_Create(800, 600);
    EGLSurface surface = eglCreateWindowSurface(display, config, window, NULL);
    
    // 绑定ArkUI组件到Surface
    UIView* uiComponent = CreateArkUIComponent();
    uiComponent->BindToSurface(window);
    
    // 配置纹理参数
    glBindTexture(GL_TEXTURE_2D, tex.textureID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    
    return tex;
}

// 修改顶点着色器
const char* vertexShaderSource = R"(
    #version 300 es
    layout(location = 0) in vec4 aPosition;
    layout(location = 1) in vec2 aTexCoord;
    out vec2 vTexCoord;
    void main() {
        gl_Position = aPosition;
        vTexCoord = aTexCoord;
    }
)";

// 修改片段着色器实现双纹理混合
const char* fragmentShaderSource = R"(
    #version 300 es
    precision mediump float;
    in vec2 vTexCoord;
    uniform samplerExternalOES uCameraTexture;
    uniform sampler2D uArkUITexture;
    out vec4 oFragColor;
    
    void main() {
        vec4 cameraColor = texture(uCameraTexture, vTexCoord);
        vec4 uiColor = texture(uArkUITexture, vTexCoord);
        
        // Alpha混合公式
        oFragColor = mix(cameraColor, uiColor, uiColor.a);
    }
)";

// 在main函数中添加：
// 创建两种纹理
TextureWrapper cameraTex = createCameraTexture();
TextureWrapper arkuiTex = createArkUITexture();

// 配置纹理单元
glUseProgram(shaderProgram);
glUniform1i(glGetUniformLocation(shaderProgram, "uCameraTexture"), 0);
glUniform1i(glGetUniformLocation(shaderProgram, "uArkUITexture"), 1);

// 渲染循环（示例）
while (!shouldClose) {
    // 更新摄像头纹理
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_EXTERNAL_OES, cameraTex.textureID);
    
    // 更新ArkUI纹理
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, arkuiTex.textureID);
    
    // 绘制操作
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    // 交换缓冲区
    eglSwapBuffers(display, surface);
}


// 启用Alpha混合
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// 设置视口匹配
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

// 顶点数据需要包含纹理坐标
GLfloat vertices[] = {
    // Position       // TexCoord
    -1.0f,  1.0f,    0.0f, 0.0f,
    -1.0f, -1.0f,    0.0f, 1.0f,
     1.0f,  1.0f,    1.0f, 0.0f,
     1.0f, -1.0f,    1.0f, 1.0f
};

// 使用三重缓冲
OH_NativeWindow_SetBufferCount(window, 3);

// 开启垂直同步

eglSwapInterval(display, 1);

// 异步纹理更新
std::thread([&](){
    while(running) {
        OH_NativeWindow_AcquireBuffer(window);
        // 更新UI内容...
        OH_NativeWindow_ReleaseBuffer(window);
    }
}).detach();





